package com.gzyz.cheetah.util;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

/**
 * redis工具类
 *
 * @author xclhove
 */
@RequiredArgsConstructor
@Component
public class RedisUtil {
    private final StringRedisTemplate stringRedisTemplate;
    
    public void setString(String key, String value) {
        ValueOperations<String, String> redis = stringRedisTemplate.opsForValue();
        redis.set(key, value);
    }
    
    public void setObjectToString(String key, Object object) {
        ValueOperations<String, String> redis = stringRedisTemplate.opsForValue();
        String value = JSONObject.toJSONString(object);
        setString(key, value);
    }
    
    public void setString(String key, String value, long offset) {
        ValueOperations<String, String> redis = stringRedisTemplate.opsForValue();
        redis.set(key, value, offset);
    }
    
    public void setObjectToString(String key, Object object, long offset) {
        ValueOperations<String, String> redis = stringRedisTemplate.opsForValue();
        String value = JSONObject.toJSONString(object);
        setString(key, value, offset);
    }
    
    public void setString(String key, String value, Duration duration) {
        ValueOperations<String, String> redis = stringRedisTemplate.opsForValue();
        redis.set(key, value, duration);
    }
    
    public void setObjectToString(String key, Object object, Duration duration) {
        ValueOperations<String, String> redis = stringRedisTemplate.opsForValue();
        String value = JSONObject.toJSONString(object);
        setString(key, value, duration);
    }
    
    public void setString(String key, String value, long offset, TimeUnit timeUnit) {
        ValueOperations<String, String> redis = stringRedisTemplate.opsForValue();
        redis.set(key, value, offset, timeUnit);
    }
    
    public void setObjectToString(String key, Object object, long offset, TimeUnit timeUnit) {
        ValueOperations<String, String> redis = stringRedisTemplate.opsForValue();
        String value = JSONObject.toJSONString(object);
        setString(key, value, offset, timeUnit);
    }
    
    public String getString(String key) {
        ValueOperations<String, String> redis = stringRedisTemplate.opsForValue();
        return redis.get(key);
    }
    
    public <T> T getStringAndParseToObject(String key, Class<T> objectClass) {
        String value = getString(key);
        T valueObject = JSONObject.parseObject(value, objectClass);
        return valueObject;
    }
    
    public void setHashString(String key, Object hashKey, String value) {
        HashOperations<String, String, String> redis = stringRedisTemplate.opsForHash();
        redis.put(key, JSON.toJSONString(hashKey), value);
    }
    
    public void setHashObject(String key, Object hashKey, Object value) {
        HashOperations<String, String, String> redis = stringRedisTemplate.opsForHash();
        redis.put(key, JSON.toJSONString(hashKey), JSON.toJSONString(value));
    }
    
    public String getHashString(String key, Object hashKey) {
        HashOperations<String, String, String> redis = stringRedisTemplate.opsForHash();
        String value = redis.get(key, JSON.toJSONString(hashKey));
        return value;
    }
    
    public <T> T getHashObjet(String key, Object hashKey, Class<T> classObject) {
        String valueString = getHashString(key, JSON.toJSONString(hashKey));
        T value = JSONObject.parseObject(valueString, classObject);
        return value;
    }
    
    public boolean delete(String key) {
        Boolean deleteSuccess = stringRedisTemplate.delete(key);
        return deleteSuccess;
    }


    public boolean deleteHashString(String key,String hashKey) {
        HashOperations<String, String, String> redis = stringRedisTemplate.opsForHash();
        Long delete = redis.delete(key, hashKey);
        return true;
    }
}